package com.zpl.member.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zpl.core.Result;
import com.zpl.member.entity.Member;
import com.zpl.member.entity.Token;
import com.zpl.member.entity.dto.MemberKeyWordDTO;
import com.zpl.member.mapper.MemberMapper;
import com.zpl.member.service.MemberService;

import com.zpl.member.utils.COSStorage;
import com.zpl.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

import static com.zpl.member.constrant.GlobalConstrant.TOKEN_PASSWORD;

/**
 * @author wangyuxin
 * @date 2020/9/28
 */
@RestController
@RequestMapping("/member")
@Api(value = "MemberController", tags = {"会员管理"})
public class MemberController {
    @Resource
    private MemberService memberService;

    @Resource
    private COSStorage cosStorage;

    @Autowired
    HttpServletRequest httpServletRequest;

    @PostMapping
    @ApiOperation(value = "1.添加会员", notes = "注册", httpMethod = "POST")
    @ApiImplicitParam(name = "member", value = "会员信息", dataType = "Member",required = true)
    public Result insert(@RequestBody Member member){
        if (memberService.insert(member)){
            return Result.success("会员添加成功");
        }
        return Result.fail(member.getMemberName()+"已存在");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "2.删除会员", notes = "通过id删除会员", httpMethod = "DELETE")
    @ApiImplicitParam(name = "id", value = "会员id", dataType = "Long")
    public Result delete(@PathVariable("id") Long id){
        if(memberService.delete(id)){
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }


    @PutMapping
    @ApiOperation(value = "3.更改会员", notes = "通过id更改会员", httpMethod = "PUT")
    @ApiImplicitParam(name = "member", value = "会员信息", dataType = "Member",required = true)
    public Result update(@RequestBody Member member) {
        if (memberService.update(member)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @GetMapping("/alllist")
    @ApiOperation(value = "4.获取所有会员", notes = "获取所有会员", httpMethod = "GET")
    public Result getAllMember(){
        List<Member> memberList=memberService.getAllMember();
        return Result.success(memberList);
    }

    @GetMapping("/list/nickname/{nickname}")
    @ApiOperation(value = "5.获取会员", notes = "通过用昵称获取会员", httpMethod = "GET")
    @ApiImplicitParam(name = "nickname", value = "昵称", dataType = "String")
    public Result selectByNickName(@PathVariable("nickname") String nickname){
        List<Member> memberList=memberService.selectByNickName(nickname);
        if (memberList.size()>0) {
            return Result.success(memberList);
        }
        return Result.fail("昵称错误!");
    }

    @GetMapping("/phone/{phone}")
    @ApiOperation(value = "7.获取会员", notes = "通过用phone获取会员", httpMethod = "GET")
    @ApiImplicitParam(name = "phone", value = "电话", dataType = "String")
    public Result getMemberByPhone(@PathVariable("phone") String phone){
        System.out.println(phone);
        Member memberByPhone = memberService.selectByPhone(phone);
        if (memberByPhone!=null) {
            return Result.success(memberByPhone);
        }
        return Result.fail("没有此手机号的会员!");
    }

    @PostMapping("/login")
    @ApiOperation(value = "8.获取账号名称", notes = "使用phone密码登录", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String",required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String")})
    public Result login(HttpServletRequest request , String phone, String password){
        System.out.println(phone);
        System.out.println(password);
        if(memberService.login(phone, password)){


            HttpSession httpSession = request.getSession();
            //Member member = memberService.getMemberByPhone(phone).get(0);
            Member member = memberService.selectByPhone(phone);
            Long id = member.getId();
            Long longObj = new Long(id);
            String Sid = longObj.toString();
            Token token=new Token();
            token.setToken(JwtUtils.sign(Sid,TOKEN_PASSWORD));
            httpSession.setAttribute("memberId",id);
            return Result.success(token);

        }else {
            return Result.fail("登录失败");
        }
    }

    @PostMapping("/memberid")
    @ApiOperation(value = "9、获取当前会员", notes = "获取当前会员", httpMethod = "POST")
//    public Result getUserIdBySession(HttpSession httpSession){
//        if(httpSession.getAttribute("userId")==null){
//            return Result.fail("用户未登录");
//        }else{
//            Long userId = (Long) httpSession.getAttribute("userId");
//            return Result.success(userId);
//        }
//    }
    public Result getUserIdByToken(String phone){
        if(memberService.selectByPhone(phone)!=null){
            Member member = memberService.selectByPhone(phone);
            return Result.success(member);
        }
        return Result.fail("未找到该用户");
    }

    @PostMapping("/memberidfeign")
    public Long getUserIdByTokenFeign(String phone){
        if(memberService.selectByPhone(phone)!=null){
            Member member = memberService.selectByPhone(phone);
            Long id = member.id;
            return id;
        }
        return null;
    }

    @GetMapping("/singlemember/{id}")
    @ApiOperation(value = "10.获取会员", notes = "通过用id获取会员", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public Result getMember(@PathVariable("id") Long id){
        Member member=memberService.getMember(id);
        return Result.success(member);
    }

    @PostMapping("/updateHeadPortrait")
    @ApiOperation(value = "11.上传头像", notes = "上传头像", httpMethod = "POST")
    @ApiImplicitParam(name = "member", value = "会员信息", dataType = "Member",required = true)
    public Result upload(MultipartFile file) throws IOException {
        String path=cosStorage.upload(file.getInputStream());
        if(!StringUtils.isEmpty(path)){
            return Result.success(path);
        }
        return Result.fail("上传失败");
    }

    @PostMapping("/page")
    @ApiOperation(value = "12、分页获取会员", notes = "分页获取列表", httpMethod = "POST")
    @ApiImplicitParam(name = "memberKeyWordDTO", value = "分页信息", dataType = "MemberKeyWordDTO")
    public Result getPage(@RequestBody MemberKeyWordDTO memberKeyWordDTO){
        IPage<Member> iPage = memberService.getMembers(memberKeyWordDTO);
        return Result.success(iPage);
    }

}
